home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13191 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.6 KB

  1. Path: mail2news.demon.co.uk!txwang
  2. From: Wang TianXing <gztxwang@public1.guangzhou.gd.cn>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: object creation from an abstract base class
  5. Date: Sun, 24 Mar 1996 16:47:51 GMT
  6. Message-ID: <199603240502.NAA06668@public1.guangzhou.gd.cn>
  7. X-NNTP-Posting-Host: txwang
  8. X-Newsreader: Forte Free Agent 1.0.82
  9. X-Mail2News-Path: public1.guangzhou.gd.cn!txwang
  10.  
  11. On 24 Mar 1996 01:16:48 GMT, Philippe Verdy
  12. <100105.3120@compuserve.com> wrote:
  13.  
  14. | Wang TianXing <gztxwang@public1.guangzhou.gd.cn> s'crit :
  15. | > | >Michael Catello wrote: 
  16. | > | >>  
  17. | > | >> [code deleted]
  18. | > | >>  
  19. | > | >> Obviously it is the CreateFoo() function that I am wondering about. In 
  20. | > | >> the actual implementation I had multiple static "Create" functions for 
  21. | > | >> the base class that would allow me to create a new object: one based 
  22. | > | >> on an enumerated token (shown above), another an existing object, as 
  23. | > | >> well as one based on the format of a datafile. My application never 
  24. | > | >> references any of the derived classes directly, except in their 
  25. | > | >> creation and definition. 
  26. | > | >>  
  27. | > | >> Is there another/better/more appropriate way to handle this type of 
  28. | > | >> object creation? Thanks for your assistance, 
  29. | > | >>  
  30. | > 
  31. | > So, the original poster should write code like this:
  32. | > 
  33. | >     CFoo *interface = new CBar;
  34. | > 
  35. | > and remove the static function CFoo::CreateFoo(FooType);
  36. | > 
  37. | No ! We need such a function in many C++ programs which have
  38. | to manage presistent collections of objects. However, such
  39. | applications do not use an enumeration but a registration
  40. | base class which is called by adding a static member to each
  41. | derived class, which will register themselves and obtain a
  42. | valid Id collected on a persistent stream. But on the final
  43. | result, managing such collections require that we have a
  44. | CFoo::CreateFoo(FooType) function in order to recreate the
  45. | collection of objects from a source which only enumerates
  46. | FooTypes. This is useful to create containers of graphic
  47. | objects with common attributes, and varying parameters
  48. | stored on a persistent stream, so that we can load and draw
  49. | the collection of objects without knowing anything on them.
  50. | Your code is legal Wang but you can generalize it to avoid
  51. | the switch() and the enumeration.
  52. | Philippe Verdy
  53.  
  54. Well, Michael seems really interested in how to handle persistent
  55. objects elegantly.  There's a good example in Borland C++: TStreamable
  56. and its related classes.  It can write pointers into a stream.
  57. Michael may want to hack in the implementation to get some ideas.
  58.  
  59. ---
  60. Wang TianXing
  61.  
  62.  
  63.